package com.springboot.file.repository.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.file.entity.FinPledgeAssetManage;
import com.springboot.file.exception.BusinessException;
import com.springboot.file.mapper.FinPledgeAssetManageMapper;
import com.springboot.file.param.req.BatchDeleteFinPledgeAssetManageReq;
import com.springboot.file.param.req.DelFinPledgeAssetManageReq;
import com.springboot.file.param.req.FindFinPledgeAssetManageByPageReq;
import com.springboot.file.param.req.FindFinPledgeAssetManageListReq;
import com.springboot.file.repository.FinPledgeAssetManageRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 可融资产融资管理表 仓库实现类
 *
 * @author yaojinlei
 * @since 2025-04-25
 */
@Slf4j
@Service
public class FinPledgeAssetManageRepositoryImpl extends ServiceImpl<FinPledgeAssetManageMapper, FinPledgeAssetManage> implements FinPledgeAssetManageRepository {

    @Override
    public Page<FinPledgeAssetManage> findFinPledgeAssetManageByPage(FindFinPledgeAssetManageByPageReq req) {
        LambdaQueryWrapper<FinPledgeAssetManage> queryWrapper = new LambdaQueryWrapper<>();
        Page<FinPledgeAssetManage> page = new Page<>(req.getPageNo(), req.getPageSize());
        FinPledgeAssetManage finPledgeAssetManage = new FinPledgeAssetManage();
        BeanUtils.copyProperties(req, finPledgeAssetManage);
        // 构建查询条件
        buildQueryCondition(queryWrapper, finPledgeAssetManage);

        return this.page(page, queryWrapper);
    }

    @Override
    public List<FinPledgeAssetManage> findFinPledgeAssetManageListByCondition(FindFinPledgeAssetManageListReq req) {
        LambdaQueryWrapper<FinPledgeAssetManage> queryWrapper = new LambdaQueryWrapper<>();
        FinPledgeAssetManage finPledgeAssetManage = new FinPledgeAssetManage();
        BeanUtils.copyProperties(req, finPledgeAssetManage);
        // 构建查询条件
        buildQueryCondition(queryWrapper, finPledgeAssetManage);

        return this.list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addFinPledgeAssetManage(FinPledgeAssetManage finPledgeAssetManage) {
        try {
            // 执行新增
            boolean success = this.save(finPledgeAssetManage);
            if (!success) {
                throw new BusinessException("新增失败");
            }
        } catch (Exception e) {
            log.error("新增可融资产融资管理表失败", e);
            throw new BusinessException("新增失败: " + e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modFinPledgeAssetManage(FinPledgeAssetManage finPledgeAssetManage) {
        try {

            // 检查主键是否存在
            if (ObjectUtil.isEmpty(finPledgeAssetManage.getFinPledgeId())) {
                throw new IllegalArgumentException("可融资产ID不能为空");
            }

            // 检查记录是否存在
            FinPledgeAssetManage existEntity = this.getById(finPledgeAssetManage.getFinPledgeId());
            if (ObjectUtil.isEmpty(existEntity)) {
                throw new BusinessException("记录不存在");
            }

            // 执行更新
            boolean success = this.updateById(finPledgeAssetManage);
            if (!success) {
                throw new BusinessException("更新失败");
            }
        } catch (Exception e) {
            log.error("更新可融资产融资管理表失败", e);
            throw new BusinessException("更新失败: " + e.getMessage());
        }
    }

    public void delFinPledgeAssetManageById(DelFinPledgeAssetManageReq req) {
        try {
            // 检查记录是否存在
            FinPledgeAssetManage existEntity = this.getById(req.getFinPledgeId());
            if (ObjectUtil.isEmpty(existEntity)) {
                throw new BusinessException("记录不存在");
            }

            // 执行删除
            boolean success = this.removeById(req.getFinPledgeId());
            if (!success) {
                throw new BusinessException("删除失败");
            }
        } catch (Exception e) {
            log.error("删除可融资产融资管理表失败", e);
            throw new BusinessException("删除失败: " + e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delFinPledgeAssetManageByCondition(Map<String, Object> conditionMap) {
        try {
            if (ObjectUtil.isEmpty(conditionMap)) {
                throw new IllegalArgumentException("删除条件不能为空");
            }
            QueryWrapper<FinPledgeAssetManage> queryWrapper = new QueryWrapper<>();
            // 设置条件
            conditionMap.forEach((key, value) -> {
                if (value != null) {
                    queryWrapper.eq(key, value);
                }
            });
            return this.baseMapper.delete(queryWrapper);
        } catch (Exception e) {
            log.error("按条件删除可融资产融资管理表失败", e);
            throw new BusinessException("按条件删除失败: " + e.getMessage());
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(BatchDeleteFinPledgeAssetManageReq req) {
        try {
            if (ObjectUtil.isEmpty(req.getFinPledgeIds())) {
                throw new IllegalArgumentException("可融资产ID列表不能为空");
            }

            // 执行批量删除
            boolean success = this.baseMapper.deleteBatchIds(req.getFinPledgeIds()) > 0;
            if (!success) {
                throw new BusinessException("批量删除失败");
            }
        } catch (Exception e) {
            log.error("批量删除可融资产融资管理表失败", e);
            throw new BusinessException("批量删除失败: " + e.getMessage());
        }
    }

    /**
     * 构建查询条件
     *
     * @param queryWrapper         查询条件构造器
     * @param finPledgeAssetManage 查询参数
     */
    private void buildQueryCondition(LambdaQueryWrapper<FinPledgeAssetManage> queryWrapper, FinPledgeAssetManage finPledgeAssetManage) {
        if (finPledgeAssetManage == null) {
            return;
        }

        // 可融资产ID
        if (ObjectUtil.isNotEmpty(finPledgeAssetManage.getFinPledgeId())) {
            queryWrapper.eq(FinPledgeAssetManage::getFinPledgeId, finPledgeAssetManage.getFinPledgeId());
        }
        // 企业ID
        if (ObjectUtil.isNotEmpty(finPledgeAssetManage.getCustId())) {
            queryWrapper.eq(FinPledgeAssetManage::getCustId, finPledgeAssetManage.getCustId());
        }
        // 企业名称
        if (ObjectUtil.isNotEmpty(finPledgeAssetManage.getCustName())) {
            queryWrapper.eq(FinPledgeAssetManage::getCustName, finPledgeAssetManage.getCustName());
        }
        // 合同编号
        if (ObjectUtil.isNotEmpty(finPledgeAssetManage.getContractNo())) {
            queryWrapper.eq(FinPledgeAssetManage::getContractNo, finPledgeAssetManage.getContractNo());
        }
        // 采购订单编号
        if (ObjectUtil.isNotEmpty(finPledgeAssetManage.getPurchaseOrderNo())) {
            queryWrapper.eq(FinPledgeAssetManage::getPurchaseOrderNo, finPledgeAssetManage.getPurchaseOrderNo());
        }
        // 应付账款凭证编号
        if (ObjectUtil.isNotEmpty(finPledgeAssetManage.getNeedPayAccEvidenceCode())) {
            queryWrapper.eq(FinPledgeAssetManage::getNeedPayAccEvidenceCode, finPledgeAssetManage.getNeedPayAccEvidenceCode());
        }
        // 是否核销(0-是,1-否)
        if (ObjectUtil.isNotEmpty(finPledgeAssetManage.getIsCancel())) {
            queryWrapper.eq(FinPledgeAssetManage::getIsCancel, finPledgeAssetManage.getIsCancel());
        }
        // 是否锁定(0-是,1-否)
        if (ObjectUtil.isNotEmpty(finPledgeAssetManage.getIsLock())) {
            queryWrapper.eq(FinPledgeAssetManage::getIsLock, finPledgeAssetManage.getIsLock());
        }
        // 金融产品编号
        if (ObjectUtil.isNotEmpty(finPledgeAssetManage.getFinProductId())) {
            queryWrapper.eq(FinPledgeAssetManage::getFinProductId, finPledgeAssetManage.getFinProductId());
        }
        // 银行授信申请ID
        if (ObjectUtil.isNotEmpty(finPledgeAssetManage.getFinCreditApplyId())) {
            queryWrapper.eq(FinPledgeAssetManage::getFinCreditApplyId, finPledgeAssetManage.getFinCreditApplyId());
        }
        // 银行支用申请ID
        if (ObjectUtil.isNotEmpty(finPledgeAssetManage.getFinUtilizeApplyId())) {
            queryWrapper.eq(FinPledgeAssetManage::getFinUtilizeApplyId, finPledgeAssetManage.getFinUtilizeApplyId());
        }
        // 上级凭证编号
        if (ObjectUtil.isNotEmpty(finPledgeAssetManage.getParentEvidenceCode())) {
            queryWrapper.eq(FinPledgeAssetManage::getParentEvidenceCode, finPledgeAssetManage.getParentEvidenceCode());
        }
        // 创建用户
        if (ObjectUtil.isNotEmpty(finPledgeAssetManage.getCreateUser())) {
            queryWrapper.eq(FinPledgeAssetManage::getCreateUser, finPledgeAssetManage.getCreateUser());
        }
        // 最新更新用户
        if (ObjectUtil.isNotEmpty(finPledgeAssetManage.getLastUpdatedUser())) {
            queryWrapper.eq(FinPledgeAssetManage::getLastUpdatedUser, finPledgeAssetManage.getLastUpdatedUser());
        }
    }
}
 